#ifndef PATHFINDING_H
#define PATHFINDING_H

#include <vector>
#include <memory>
#include "Singleton.h"
#include "PathNode.h"
#include "World.h"
#include "Vec2.h"

namespace Engine
{
	class Pathfinding : public NonCopyable
	{
	private:
		typedef std::shared_ptr<Vec2> SPv2;
		typedef std::shared_ptr<PathNode> SPpn;

		SPpn m_startNode, m_goalNode;

		std::vector<SPpn> m_openList;
		std::vector<SPpn> m_closedList;
		std::vector<SPv2> m_path;

		Pathfinding();
		friend class Singleton<Pathfinding>;

		void AddNode(int x, int y, float newGcost, PathNode* _parent);
		std::shared_ptr<PathNode> GetNextNode();
		void SearchPath();

	public:
		bool m_goalFound;

		bool AStar(Vec2 startPos, Vec2 targetPos);

		bool FindPath(Vec2 currentPos, Vec2 targetPos);
		Vec2 NextPathPos();

		int GetPathSize() { return m_path.size(); }

		// ---> converting from world to grid coords
		Vec2 ToGridCoords(Vec2 worldCoords);

		// ---> convert from grid to world coords
		Vec2 ToWorldCoords(Vec2 gridCoords);
	};

	typedef Singleton<Pathfinding> ThePathfinding;
}

#endif